package com.vegetable.modules.controller.baseData;


import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.baseData.ProductSpec;
import com.vegetable.modules.service.baseData.IProductSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 商品规格 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-05
 */
@Slf4j
@RestController
@RequestMapping("/productSpec")
@AllArgsConstructor
@Api(value = "商品规格", tags = "商品规格")
public class ProductSpecController {

    private IProductSpecService productSpecService;
    /**
     * 所有商品规格列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据产地商品id查询商品规格列表", notes = "根据产地商品id查询商品规格列表")
    public R list(@RequestParam Map<String, Object> params){
        String producerId = (String)params.get("producerId");
        if (StringUtils.isBlank(producerId)){
            return R.fail("商品id不能为空");
        }
        log.info("根据产地商品id查询商品规格列表{}", JSON.toJSONString(params));
        PageUtils page = productSpecService.queryPage(params);

        return R.data( page);
    }

    /**
     * 新增商品规格
     */
    @SysLog(value = "新增商品规格")
    @PostMapping("/save")
    @ApiOperation(value = "新增产地商品的商品规格", notes = "新增产地商品的商品规格")
    public R save(@RequestBody ProductSpec productSpec){
        if (productSpec.getProducerId() == null){
            return R.fail("商品id不能为空");
        }
        productSpecService.save(productSpec);
        return R.status(true);
    }

    /**
     * 修改商品规格
     */
    @SysLog(value = "修改商品规格")
    @PostMapping("/update")
    @ApiOperation(value = "修改产地商品的商品规格", notes = "修改产地商品的商品规格")
    public R update(@RequestBody ProductSpec productSpec){
        if (productSpec.getProducerId() == null || productSpec.getId() == null){
            return R.fail("商品id和商品规格id都不能为空");
        }
        productSpecService.updateById(productSpec);
        return R.status(true);
    }

    /**
     * 删除商品规格
     */
    @SysLog(value = "删除商品规格")
    @GetMapping("/delete")
    @ApiOperation(value = "根据商品规格id删除商品规格", notes = "根据商品规格id删除商品规格")
    public R delete(Long id){
        productSpecService.removeById(id);
        return R.status(true);
    }
}
